<?php
    /**
    * Product Categories get list
    * return in Json format
    * @author trungdt
    * @since Sep 27, 2011
    * 
    * Change logs
    * Oct 2, 2011 - trungdt - Make this action compatible with product edit page
    */
    if (!defined('EXPONENT')) exit('');
    if (exponent_permissions_check(array('administrate','configure'),$loc)){
        $sql_where = '1';
        $search_email = '';
        $from_date = 0;
        $to_date = 0;
        $mail_status = 0;
        $page=1;
        $sort="";

        if(isset($_REQUEST['search_name'])&&isset($_REQUEST['search_detail']))
        {
            $search_detail = $_REQUEST['search_detail'];  
            $search_name = $_REQUEST['search_name'];  
            $sql_where .= " AND $search_name LIKE '%{$search_detail}%'"; 
        }
        if(isset($_REQUEST['mail_status']))
        {
            if($_REQUEST['mail_status']==0)
            {
                $table='mailsystem_mail';
            }
            else if($_REQUEST['mail_status']==1)
            {
                $table='mailsystem_mail_sent';
            }
            else
            {
                $table='mailsystem_mail_error';
            }
            
            
        }
        if(isset($_REQUEST['group']))
        {
            if($_REQUEST['group']!='All')// All
            {
                $group = trim($_REQUEST['group']);   
                $sql_where .= " AND `group` = '{$group}'"; 
            }
        }
        if(isset($_REQUEST['from_date']))
        {   
            $from_date = strtotime($_REQUEST['from_date']);
            $sql_where.=" AND (date_send >= {$from_date})";
        }
        if(isset($_REQUEST['to_date']))
        {   
            $to_date = strtotime($_REQUEST['to_date']);
            $sql_where.=" AND (date_send <= {$to_date})";
        }
        //sort date_insert
        if (isset($_REQUEST['sortName']))
        {
            $sort=$_REQUEST['sortName'];
            if ($sort == "date_send_formater")
                $sort = "date_send";
            $sortOrder="ASC";
            if (isset($_REQUEST['sortOrder']) && ($_REQUEST['sortOrder'] != "asc"))
                $sortOrder = "DESC";
            $sort = " ORDER BY `{$sort}` {$sortOrder}";
        }
        else
            $sort = "ORDER BY id";

        if (isset($_REQUEST['page']))
            $page=intval($_REQUEST['page']);
        $pageSize=10;
        if (isset($_REQUEST['pageSize']))
            $pageSize=intval($_REQUEST['pageSize']);
        $start=($page-1)*$pageSize;
        $data=$db->selectObjects($table,"{$sql_where} {$sort} limit {$start},{$pageSize}");

        foreach ($data as $k=>$v)
        {
            $v->date_insert_formater = date("F j, Y g:i a", $v->date_insert);
            if($v->date_send)
                $v->date_send_formater = date("F j, Y g:i a", $v->date_send);
            else
                $v->date_send_formater='';
        }
        $ret_object = null;
        $ret_object->total = $db->countObjects('mailsystem_mail',"{$sql_where} {$sort} limit {$start},{$pageSize}");
        $ret_object->rows = $data;
        echo json_encode($ret_object);
    }
    die();
?>